package video.zerocopy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class OldServer {

    public static void main(String[] args) throws Exception {
        long startTime = System.currentTimeMillis();
        InetSocketAddress inetSocketAddress = new InetSocketAddress(8899);

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.bind(inetSocketAddress);

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024 * 500);

        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            socketChannel.configureBlocking(true);

            int readCount = 0;
            int sum = 0;
            while (-1 != readCount) {
                try {
                    readCount = socketChannel.read(byteBuffer);
                    sum+=readCount;
                } catch (IOException e) {
                    System.out.println(socketChannel + " ： 下线");
                    long endTime = System.currentTimeMillis();
                    System.out.println("传输文件大小："+sum+"\t文件传输时间：" + (endTime - startTime));
                    startTime = endTime;
                    socketChannel.close();
                    break;
                }
                byteBuffer.rewind();
            }
        }

    }
}
